/**
 * ECP Web开发 通用控件.
 * Copyright(c) 2012-2020 YGSoft.Inc
 * author:wugang5@ygsoft.com
 * date:2016-7-20
 */

 define(['bootstrap'], function(){
	 (function($){
		//简易的预览组件 基于模态窗
		 $.preview = function(options){
			 var defaults = {
				 width :'80%',
				 height: '800px',
				 fullScreenMode:false,
				 iframeMode:{},
				 dialogShow: function() {}, 
	             dialogShown: function() {}, 
	             dialogHide: function() {}, 
	             dialogHidden: function() {},
	             bootstrapModalOption : {}
			 };
			 options = $.extend(defaults, options || {});
			 var modalID =new Date().getTime();
			 var template =  '<div class="colorModal modal fade " id="preivew_{ID}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel_t_{ID}" aria-hidden="false" style="display: block;">'
					 		+	'<div class="modal-dialog " style="width:{width}">'
			 				+		'<div class="modal-content">'
			 				+			'<div class="modal-header">'
			 				+				'<button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-top: -10px;">×</button>'
			 				+			'</div>'
			 				+			'<div class="modal-body" id="body_t_{ID}" style="height:{height}">'
			 				+				'<iframe id="iframe_t_{ID}" src="{src}" width="100%" height="100%" frameborder="0">'
			 				+			'</div>'
			 				+		'</div>'
			 				+	'</div>'
			 				+ '</div>';
			  $.fn.extend({
                closeDialog: function(modal) {
                    var modalObj = modal;
                    modalObj.modal('hide');
                }
			  });
			  
			  $('body').append(template.replace(/{ID}/g, modalID).replace(/{body}/g, '').replace(/{width}/g, options.width).replace(/{height}/g, options.height).replace(/{src}/g, options.iframeMode.url || ''));
			  var $modal = $('#preivew_'+modalID);
			  $('body').attr("data-target", modalID);
			  if(options.iframeMode.options){
				  $modal.find("#iframe_t_" + modalID)[0].contentWindow._options = options.iframeMode.options;
              }
			  $modal.on('show.bs.modal', function(){
				  //初始化全屏处理
				  toFullScreen();
				  options.dialogShow();
			  });
			  $modal.on('shown.bs.modal', function(){
				  options.dialogShown();
			  });
			  $modal.on('hide.bs.modal', function(){
				  options.dialogHide();
				  $('body').removeAttr("data-target");
                  $modal.remove();
			  });
			  $modal.on('hidden.bs.modal', function(){
				  options.dialogHiden();
			  });
			  options.bootstrapModalOption.backdrop = 'static';
			  $modal.modal(options.bootstrapModalOption);
			  
				//初始化默认全屏
				function  toFullScreen() {
					if(options.fullScreenMode){
						var $window = $(window),
							$body = $modal.find(".modal-body"),
							$dialog = $modal.find(".modal-dialog");
						$dialog.css({"margin-top":0,"margin-bottom":0});
						$body.height($window.height() - 63);
						$dialog.width($window.width());
					}
				}
				//重置窗体
				$(window).on("resize", function(){
					toFullScreen();
				});
			  return $modal;
		 }
	})(jQuery);
	return {
		show : $.preview
	};
 });